package com.mycar2w.cloud.service.repository;

import com.mycar2w.cloud.entity.ApplieOrder;
import com.mycar2w.cloud.service.constant.ConstantInfo;
import com.umuw.cloud.core.repository.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * created time 2017/4/4
 *
 * @author ljc
 * @version V1.0
 */
@Repository
public interface ApplieOrderRepository extends BaseRepository<ApplieOrder>{

    List<ApplieOrder> findByUserId(Long userId);

    ApplieOrder findById(Long id);

    default Page<ApplieOrder> findByIsOver(Integer category, Integer pageIndex){
        Pageable pageable=new PageRequest(pageIndex, ConstantInfo.PAGE_SIZE,new Sort(Sort.Direction.DESC,"id"));
        Specification<ApplieOrder> spec=(root,query,cb)->{
            Path<Integer> isOrver=root.get("isOver");
            Predicate pre=isOrver.in(category);
            return pre;
        };
        return findAll(spec,pageable);
    }

   default Page<ApplieOrder> getFromLikeCarName(String carName, Integer pageIndex){
       Pageable pageable=new PageRequest(pageIndex, ConstantInfo.PAGE_SIZE,new Sort(Sort.Direction.DESC,"id"));
       Specification<ApplieOrder> spec=(root,query,cb)->{
           Path<String> carNamePath=root.get("carXinghao");
           Predicate pre=cb.like(carNamePath,"%"+carName+"%");
           return pre;
       };
       return findAll(spec,pageable);

   }
}
